package com.atguigu.gulimall.search.controller;

import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.to.SkuEsModel;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @create: 2024/8/29
 * @author: gyhe10
 * @description:
 **/
@Slf4j
@RequestMapping("/search/save")
@RestController
public class EsSaveController {

    @Resource
    private ProductSaveService productSaveService;

    /**
     * 上架商品，添加商品文档到 ES 中的商品索引
     *
     * @param skuEsModels
     * @return
     */
    @PostMapping("/product")
    public R productStatusToUp(@RequestBody List<SkuEsModel> skuEsModels) {
        boolean hasFailures = false;
        try {
            hasFailures = productSaveService.productStatusToUp(skuEsModels);
        } catch (IOException e) {
            log.error("EsSaveController 商品上架错误：{}", e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (hasFailures) {
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        } else {
            return R.ok();
        }
    }
}
